/*
 *  Copyright 2011.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.queryhandler.n.client;

import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.swing.JFrame;
import org.opu.queryhandler.n.client.services.ConnectionService;
import org.opu.queryhandler.n.client.services.DataRepresentationService;
import org.opu.queryhandler.n.client.services.DatabaseService;
import org.opu.queryhandler.n.client.services.DriverService;
import org.opu.queryhandler.n.client.ui.actions.About;
import org.opu.queryhandler.n.client.ui.actions.AbstractAction;
import org.opu.queryhandler.n.client.ui.actions.DbConnect;
import org.opu.queryhandler.n.client.ui.actions.Exit;
import org.opu.queryhandler.n.client.ui.actions.Help;
import org.opu.queryhandler.n.client.ui.actions.SaveAction;
import org.opu.queryhandler.n.client.ui.actions.db.DbAction;
import org.opu.queryhandler.n.client.ui.actions.db.ExecuteQueryAction;
import org.opu.queryhandler.n.util.TextResource;

/**
 *
 */
public class EventBus {

    /** All available actions */
    private Map<String, AbstractAction> actions;

    /** Actions that fires when application closed */
    private final Set<ActionListener> listeners;

    /** Actions that depends of db connect, so value of database connect in this
     * actions must chenging each time, as change database connect in model. */
    private final Map<String, DbAction> connectActions;

    private final DatabaseService dbService;
    private final DriverService driverService;
    private final ConnectionService connectionService;
    private final DataRepresentationService representationService;

    public EventBus(DatabaseService dbService, DriverService driverService,
            ConnectionService connectionService,
            DataRepresentationService representationService) {
        this.dbService = dbService;
        this.driverService = driverService;
        this.connectionService = connectionService;
        this.representationService = representationService;
        this.listeners = new HashSet<ActionListener>();
        this.actions = new HashMap<String, AbstractAction>();
        this.connectActions = DbAction.getActions(dbService);

        init();
    }

    public boolean addExitListeners(Collection<ActionListener> e) {
        return listeners.addAll(e);
    }

    /**
     *  Adding all actions to {@link #actions} map
     */
    private void init(){
        ExecuteQueryAction qa = new ExecuteQueryAction(dbService, representationService);
        connectActions.put(ExecuteQueryAction.MNAME, qa);

        actions.put(About.MNAME, new About());
        actions.put(SaveAction.MNAME, new SaveAction());
        actions.put(ExecuteQueryAction.MNAME, qa);
        actions.put(DbConnect.MNAME, new DbConnect(dbService, driverService,
                connectionService, connectActions));
        actions.put(Exit.MNAME, new Exit(listeners));
        actions.put(Help.MNAME, new Help());

        actions.putAll(connectActions);
    }

    public AbstractAction getAction(String name){
        return actions.get(name);
    }

    public void setTextResource(TextResource tr){
        ((ExecuteQueryAction)getAction(ExecuteQueryAction.MNAME)).setTextResource(tr);
    }

    public Exit getExitAction() {
        return (Exit) actions.get(Exit.MNAME);
    }

    public Collection<DbAction> dbActions(){
        return connectActions.values();
    }
}
